package com.github.easyms.common.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.easyms.common.constant.EasymsServiceStatus;
import com.github.easyms.common.constant.GlobalConstants;
import com.github.easyms.common.exception.EasymsException;
import com.github.easyms.common.util.StringHelper;
import com.github.easyms.common.vo.EasymsPage;
import com.github.easyms.common.vo.PageParams;
import com.github.easyms.common.vo.ResultPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;

/**
 * 基础控制器
 *
 * @author Leo.Liao
 * @date 2020-04-11 12:38:58
 */
public abstract class BaseController<T extends Serializable> {

    protected IService<T> service;

    /**
     * 分页查询
     * @param pageParams
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping(GlobalConstants.Url.LIST)
    public ResponseEntity<ResultPage<T>> list(PageParams pageParams, T conditions){

        // 分页查询对象
        EasymsPage<T> queryPage = new EasymsPage<>();
        // 当前页，页大小，查询条件
        queryPage.setCurrent(pageParams.getPage());
        queryPage.setSize(pageParams.getRows());
        queryPage.setCondition(conditions);
        // 排序
        if(!StringUtils.isEmpty(pageParams.getOrder()) && !StringUtils.isEmpty(pageParams.getOrder())){
            String [] sorts = pageParams.getSort().split(GlobalConstants.OtherConst.GNU_SED);
            String [] orders = pageParams.getOrder().split(GlobalConstants.OtherConst.GNU_SED);
            if(orders.length != 0 && orders.length == sorts.length){
                int sortIndex = 0;
                for(String sort : sorts){
                    OrderItem orderItem = new OrderItem();
                    // TODO 这里有SQL注入的风险
                    orderItem.setColumn(StringHelper.humpToLine(sort));
                    orderItem.setAsc(GlobalConstants.OtherConst.ASC.equals(orders[sortIndex++]));
                    queryPage.addOrder(orderItem);
                }
            }
        }
        EasymsPage<T> pageData = service.page(queryPage);
        return ResponseEntity.ok(new ResultPage(pageData.getRecords(),pageData.getTotal(),pageParams.getPage()));
    }

    /**
     * 插入
     * @param entity
     * @return
     */
    @ApiOperation(value = "插入")
    @PostMapping(GlobalConstants.Url.INSERT)
    public ResponseEntity<T> insert(@RequestBody T entity){
        if(!service.save(entity)){
            throw new EasymsException(EasymsServiceStatus.ERROR);
        }
        return ResponseEntity.ok(entity);
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @ApiOperation(value = "更新")
    @PostMapping(GlobalConstants.Url.UPDATE)
    public ResponseEntity<T> update(@RequestBody T entity){
        if(!service.updateById(entity)){
            throw new EasymsException(EasymsServiceStatus.ERROR);
        }
        return ResponseEntity.ok(entity);
    }

    /**
     * 删除
     * @param entity
     * @return
     */
    @ApiOperation(value = "删除")
    @PostMapping(GlobalConstants.Url.DELETE)
    public ResponseEntity<T> delete(@RequestBody T entity){
        if(!service.removeById(entity)){
            throw new EasymsException(EasymsServiceStatus.ERROR);
        }
        return ResponseEntity.ok(entity);
    }

    /**
     * 下拉列表数据
     * @param entity
     * @param q 固定查询字段
     * @return
     */
    @ApiOperation(value = "下拉列表数据")
    @GetMapping(GlobalConstants.Url.LIST_COMBOBOX)
    public ResponseEntity<List<T>> listCombobox(T entity, String q){
        return ResponseEntity.ok(service.list());
    }

}
